package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.domain.vo.TWardVo;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TWard;
import com.ruoyi.system.service.ITWardService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 病房信息Controller
 * 
 * @author 杨一迪
 * @date 2024-12-28
 */
@Controller
@RequestMapping("/system/ward")
public class TWardController extends BaseController
{
    private String prefix = "system/ward";

    @Resource
    private ISysDeptService sysDeptService;

    @Autowired
    private ITWardService tWardService;

    @RequiresPermissions("system:ward:view")
    @GetMapping()
    public String ward()
    {

        return prefix + "/ward";
    }

    /**
     * 查询病房信息列表
     */
    @RequiresPermissions("system:ward:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TWard tWard)
    {
        startPage();
        List<TWard> list = tWardService.selectTWardList(tWard);
        List<TWardVo> tWardVos = new ArrayList<>();
        list.forEach(ward -> {
            TWardVo tWardVo = new TWardVo();
            BeanUtils.copyProperties(ward,tWardVo);
            SysDept sysDept = sysDeptService.selectDeptById(ward.getDepartmentId());
            tWardVo.setDeptName(sysDept.getDeptName());
            tWardVos.add(tWardVo);
        });
        return getDataTable(tWardVos);
    }

    /**
     * 导出病房信息列表
     */
    @RequiresPermissions("system:ward:export")
    @Log(title = "病房信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TWard tWard)
    {
        List<TWard> list = tWardService.selectTWardList(tWard);
        ExcelUtil<TWard> util = new ExcelUtil<TWard>(TWard.class);
        return util.exportExcel(list, "病房信息数据");
    }

    /**
     * 新增病房信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<SysDept> sysDepts = sysDeptService.selectDeptList(new SysDept());
        mmap.put("depts",sysDepts);
        return prefix + "/add";
    }

    /**
     * 新增保存病房信息
     */
    @RequiresPermissions("system:ward:add")
    @Log(title = "病房信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TWard tWard)
    {
        return toAjax(tWardService.insertTWard(tWard));
    }

    /**
     * 修改病房信息
     */
    @RequiresPermissions("system:ward:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TWard tWard = tWardService.selectTWardById(id);
        mmap.put("tWard", tWard);
        List<SysDept> sysDepts = sysDeptService.selectDeptList(new SysDept());
        mmap.put("depts",sysDepts);
        return prefix + "/edit";
    }

    /**
     * 修改保存病房信息
     */
    @RequiresPermissions("system:ward:edit")
    @Log(title = "病房信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TWard tWard)
    {
        return toAjax(tWardService.updateTWard(tWard));
    }

    /**
     * 删除病房信息
     */
    @RequiresPermissions("system:ward:remove")
    @Log(title = "病房信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tWardService.softDeleteTWardByIds(ids));
    }



    @PostMapping( "/selectWardByDeptId")
    @ResponseBody
    public AjaxResult selectWardByDeptId(String deptId)
    {
        return AjaxResult.success(tWardService.selectTWardListByDeptId(Long.valueOf(deptId)));
    }
}
